package ua.com.fesvlast.hotel.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import ua.com.fesvlast.hotel.model.connectionpool.ConnectionManager;
import ua.com.fesvlast.hotel.model.enteties.HotelRooms;

public class HotelRoomsDAO {
	private static Connection conn=ConnectionManager.getConnection();
	
	
	public  List<HotelRooms> getListRooms() throws SQLException{
		List<HotelRooms> list =new ArrayList<HotelRooms>();
		
		String sql="SELECT hotelRoomId,article, classRoom, price, status, description, smallPhoto, bigPhoto, reservedTime FROM hotelRooms";
		try(
				Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
				ResultSet rs=stmt.executeQuery(sql);
			
			){
			
			while(rs.next()){
				HotelRooms room =new HotelRooms();
				room.setHotelRoomId(rs.getInt("hotelRoomId"));
				room.setArticle(rs.getString("article"));
				room.setClassRoom(rs.getString("classRoom"));
				room.setPrice(rs.getDouble("price"));
				room.setStatus(rs.getString("status"));
				room.setDescription(rs.getString("description"));
				room.setSmallPhoto(rs.getString("smallPhoto"));
				room.setBigPhoto(rs.getString("bigPhoto"));
				room.setReservedTime(rs.getString("reservedTime"));
				
				
				list.add(room);
			}
			ConnectionManager.releaseConnection(conn);
		
		}
		return list;
	}
	
	public HotelRooms getChosenRoom(int id){
		
		String sql="SELECT hotelRoomId, article, classRoom, price, status, description, smallPhoto, bigPhoto, reservedTime FROM hotelRooms"+
		" WHERE hotelRoomId=?";
		
		ResultSet rs=null;
		HotelRooms room =null;
		PreparedStatement stmt=null;
		try{
			stmt=conn.prepareStatement(sql);
				stmt.setInt(1, id);
				
			rs=stmt.executeQuery();
			
			if(rs.next()){
				room =new HotelRooms();
				room.setHotelRoomId(id);
				room.setArticle(rs.getString("article"));
				room.setClassRoom(rs.getString("classRoom"));
				room.setPrice(rs.getDouble("price"));
				room.setStatus(rs.getString("status"));
				room.setDescription(rs.getString("description"));
				room.setSmallPhoto(rs.getString("smallPhoto"));
				room.setBigPhoto(rs.getString("bigPhoto"));
				room.setReservedTime(rs.getString("reservedTime"));
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();		
		}finally{
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(stmt!=null){
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		ConnectionManager.releaseConnection(conn);
		return room;
	}
}
